package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import my.soft.aiteacher.teacher.pojo.KnowledgeDo;
import my.soft.aiteacher.teacher.mapper.KnowledgeMapper;
import my.soft.aiteacher.teacher.pojo.KnowledgeVo;
import my.soft.aiteacher.teacher.service.IKnowledgeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 知识点关联 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2025-06-06
 */
@Service
public class KnowledgeServiceImpl extends ServiceImpl<KnowledgeMapper, KnowledgeDo> implements IKnowledgeService {

    @Resource
    private KnowledgeMapper knowledgeMapper;


    @Override
    public void upload(List<KnowledgeVo> knowledgeVos) {
        knowledgeMapper.delete(new QueryWrapper<KnowledgeDo>().in("file_id",knowledgeVos.stream().map(KnowledgeVo::getFileId).collect(Collectors.toList())));
        List<KnowledgeDo> knowledgeDoList = new ArrayList<>();
        for (KnowledgeVo knowledgeVo : knowledgeVos) {
            String matchedTags = String.join(",", knowledgeVo.getMatchedTags());
            String applicationScenarios = knowledgeVo.getApplicationScenarios().stream()
                    .map(Objects::toString)
                    .collect(Collectors.joining(","));
            String extendedKeyWords = String.join(",", knowledgeVo.getExtendedKeywords());
            KnowledgeDo knowledgeDo = KnowledgeDo.builder()
                    .outline(knowledgeVo.getOutline())
                    .matchedTags(matchedTags)
                    .technicalSummary(knowledgeVo.getTechnicalSummary())
                    .applicationScenarios(applicationScenarios)
                    .extendedKeywords(extendedKeyWords)
                    .fileId(knowledgeVo.getFileId())
                    .build();
            knowledgeDoList.add(knowledgeDo);
        }
        knowledgeMapper.insert(knowledgeDoList);
    }
}
